package com.museum.guide.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.guide.entity.Collections;
import com.museum.guide.mapper.CollectionsMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class CollectionsService extends ServiceImpl<CollectionsMapper, Collections> {

    /**
     * 分页查询藏品信息
     */
    public Page<Collections> page(Page<Collections> page, String name, Long categoryId) {
        LambdaQueryWrapper<Collections> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), Collections::getName, name)
                .eq(categoryId != null, Collections::getCategoryId, categoryId)
                .orderByDesc(Collections::getCreateTime);
        return page(page, wrapper);
    }

    /**
     * 新增藏品
     */
    public void saveCollections(Collections collections) {
        collections.setCreateTime(LocalDateTime.now());
        collections.setUpdateTime(LocalDateTime.now());
        save(collections);
    }

    /**
     * 更新藏品信息
     */
    public void updateCollections(Collections collections) {
        collections.setUpdateTime(LocalDateTime.now());
        updateById(collections);
    }

    /**
     * 删除藏品
     */
    public void removeCollections(Long id) {
        removeById(id);
    }
}